﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Citiport.Util.Language;
using System.Collections;
using System.Text;

public partial class city_best : System.Web.UI.Page
{
    public string ct_name;
    int no;

    protected void Page_Load(object sender, EventArgs e)
    {
        no=int.Parse(Request["no"]);

        if (Request["local"] != null) this.setlocal();


        ctpDataContext db = new ctpDataContext();
        ct_profile ct = db.ct_profiles.Single(p => p.no == no && p.language_no == (int)Session["now_user_lang_no"]);

        ct_name = ct.title.Trim();
        this.LinkButton1.Text = "I am a " + ct_name + " local";

        //if(ct.ct_s_amount<7) Response.Redirect("city_creating.aspx?no=" + no.ToString());


        var cts = from p in db.ct_s_profiles
                  where p.valid==true && p.ct_no == no && p.language_no == (int)Session["now_user_lang_no"]
                  orderby p.recommend_to_everyone descending
                  select p;

        ct.ct_s_amount = cts.Count();
        db.SubmitChanges();


        //var cts1 = from p in db.ct_s_profiles
        //          where p.valid == true && p.ct_no == no && p.language_no == (int)Session["now_user_lang_no"]
        //          orderby p.ranking_local descending
        //          select p;

        foreach (ct_s_profile c in cts)
        {
            if (c.main_pic_fn == null)
                c.main_pic_fn = "coming.gif";
            else
            {
                c.main_pic_fn = c.main_pic_fn.Trim();
            }
                //c.title = c.title.Substring(0, 9);
            
        }

        //foreach (ct_s_profile c in cts1)
        //{
        //    if (c.main_pic_fn == null)
        //        c.main_pic_fn = "coming.gif";
        //    else
        //    {
        //        c.main_pic_fn = c.main_pic_fn.Trim();
        //    }
        //    c.title = c.title.Substring(0, 9);

        //}

        //bind local top10
        var local10 = from p in db.ct_s_profiles
                  where p.valid == true && p.ct_no == no && p.language_no == (int)Session["now_user_lang_no"]
                  orderby p.recommend_to_locals descending
                  select p;
        //init helper class
        GeoLingManager glManager = new GeoLingManager(Request, Response);
        String _lang = glManager.getLang();
        DatabaseUtil dbUtil = new DatabaseUtil();
        //Get user's preferred langugage in int
        _lang = dbUtil.getLanguageNo(_lang).ToString();
        ArrayList al = new ArrayList();

        foreach (ct_s_profile c in local10.Take(10))
        {
            if (c.main_pic_fn == null)
                c.main_pic_fn = "coming.gif";
            else
            {
                c.main_pic_fn = c.main_pic_fn.Trim();
            }
            //Response.Write(c.no+ ",");
            al.Add(c.no);
            int no_ = c.no;
        }
        //Get all translated text
        Hashtable postsTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, DataUtil.ObjsToInts(al.ToArray()));
        //Replace with translated text
        foreach (ct_s_profile c in local10.Take(10))
        {
            c.title = dbUtil.getPropertyPost(c.title, postsTable, c.no, _lang);
        }

        //bind traveller top10
        var traveller10 = from p in db.ct_s_profiles
                          where p.valid == true && p.ct_no == no && p.language_no == (int)Session["now_user_lang_no"]
                          orderby p.recommend_to_first descending
                          select p;
        al = new ArrayList();
        foreach (ct_s_profile c in traveller10.Take(10))
        {
            if (c.main_pic_fn == null)
                c.main_pic_fn = "coming.gif";
            else
            {
                c.main_pic_fn = c.main_pic_fn.Trim();
            }
            al.Add(c.no);
        }
        postsTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, DataUtil.ObjsToInts(al.ToArray()));
        foreach (ct_s_profile p in traveller10.Take(10))
        {
            p.title = dbUtil.getPropertyPost(p.title, postsTable, p.no, _lang);
        }
        //bind forum
        var foudata = from p in db.ct_s_forums
                      join q in db.ct_s_profiles on p.ct_s_no equals q.no
                      where p.reply_to == 0 && p.ct_no == no && q.valid==true
                      orderby p.last_post descending
                      select new
                      {
                          p.no,
                          p.title,
                          p.reply_amount,
                          ct_s_title=q.title,
                          profile_picture=p.user.profile_picture.Trim(),
                          p.create_user_no,
                          profile_no  = q.no 
                      };
       
        ArrayList al2 = new ArrayList();

        foreach (var c in foudata)
        {
            al2.Add(c.profile_no);
        }
        //Get all translated text
        Hashtable postsTable2 = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, DataUtil.ObjsToInts(al2.ToArray()));
        //Replace with translated text
		  foudata = from p in db.ct_s_forums
					  join q in db.ct_s_profiles on p.ct_s_no equals q.no
					  where p.reply_to == 0 && p.ct_no == no && q.valid==true
					  orderby p.last_post descending
					  select new
					  {
						  p.no,
						  p.title,
						  p.reply_amount,
						  ct_s_title=dbUtil.getPropertyPost(q.title, postsTable2, q.no, _lang),
						  profile_picture=p.user.profile_picture.Trim(),
						  p.create_user_no,
						  profile_no  = q.no 
					  };

        this.GridView3.DataSource = foudata.Distinct();


        this.DataList2.DataSource = traveller10.Take(10);
        //this.DataList3.DataSource = cts1.Take(10);
        this.GridView1.DataSource = cts;
        this.GridView2.DataSource = local10.Take(10);


       // ct_s_profile hot = db.ct_s_profiles.Single(p=>p.ranking>=7&&p.ct_no==no&&p.valid==true);
        ct_s_profile hot=cts.First();

        if (hot.main_pic_fn != null)
        {
            this.Image5.ImageUrl = "city_imgs/2/" + hot.main_pic_fn.Trim();
        }
        else
            this.Image5.Visible = false;

        this.HyperLink8.Text = hot.title.Trim();
        this.HyperLink8.NavigateUrl="city_s.aspx?no="+hot.no.ToString();
        this.Label7.Text = hot.ct_s_type.title.Trim();
        //this.HyperLink7.Text=hot.user.nickname.Trim();
        //this.HyperLink7.NavigateUrl="user_profile.aspx?no="+hot.create_user_no.ToString();

        //if(hot.user.profile_picture!=null)
        //    this.Image6.ImageUrl="user_imgs/1/"+hot.user.profile_picture.Trim();
        //else
        //    this.Image6.Visible=false;





        var type = from p in db.ct_s_types
                   where p.language_no == (int)Session["now_user_lang_no"]
                   orderby p.no descending
                   select new
                   {
                       title=p.title,
                       to="city1.aspx?type="+p.no+"&no="+no.ToString()

                   };
        

        this.DataList1.DataSource = type;

        var locals = from p in db.users
                     where p.ct_no == no
                     select p;

        foreach (user u in locals)
        {
            u.nickname = u.nickname.Trim();
            if (u.nickname.Length > 10)
                u.nickname = u.nickname.Substring(0, 10);

            if (u.profile_picture != null)
            {
                u.profile_picture = u.profile_picture.Trim();
            }
            else
            {
                u.profile_picture = "coming.gif";
            }

        }

        this.DataList3.DataSource = locals;

        //var travelers = from p in db.ct_s_reviews
        //                where p.ct_no == no && p.been_there == true && p.is_local == false
        //                select new
        //                {
        //                    create_user_no=p.create_user_no,
        //                    profile_picture=p.user.profile_picture,
        //                    nickname=p.user.nickname
        //                };

        //foreach (var r in travelers)
        //{
        //    if (r.profile_picture != null)
        //    {
        //       // r.profile_picture =r.profile_picture.Trim();
        //    }
        //    else
        //    {
        //       //r.profile_picture = "coming.gif";
        //    }
        //}

        //this.DataList4.DataSource = travelers.Distinct();
 

        this.DataBind();


    }


    void setlocal()
    {
        ctpDataContext db = new ctpDataContext();

        user u = db.users.Single(p => p.no == (int)Session["userno"]);
        u.ct_no = no;
        db.SubmitChanges();

        Response.Redirect("city.aspx?no=" + no);

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "city.aspx?no=" + no + "&local=true");
        this.setlocal();
    }
    protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }    
}
